<?php
/**
 * @since 08.07.2011
 * @author Akulov Denis <akulov.d.g@gmail.com>
 */

session_start();

xhprof_enable();

define('CORE', '/opt/http/cms736/trunk/core/');

require_once CORE.'classes/Config/Interface.php';
require_once CORE.'classes/Config.php';
require_once CORE.'Autoload.php';
require_once 'config.php';

$objAutoload = new Autoload($objConfig);
$objAutoload->register();

$objContainer = new Container();
$objContainer->set('Config', $objConfig);
$objContainer->set('Hook_View', new Hook_View($objConfig));
$objContainer->set('MVC_ModelLoader', new MVC_ModelLoader($objContainer));
$objContainer->set('DB_Manager', new DB_Manager($objConfig));
$objContainer->set('POST', new Container_OnlyRead($_POST));
$objContainer->set('GET', new Container_OnlyRead($_GET));
$objContainer->set('SERVER', new Container_OnlyRead($_SERVER));
$objContainer->set('SESSION', new Container($_SESSION));

$objMvcBuilder = new MVC_Builder($objConfig, $objContainer);

$objMvcCarcass = new MVC_Carcass(
    $objConfig->getAttribute('FW_PATHS', 'CARCASS'),
    $_SERVER['REQUEST_URI'],
    $objConfig->getConfig('CARCASS_RULES')
);
$arMvcNodes = $objMvcCarcass->getMvcNodes();

$objMvcPath = new MVC_Path();
$objMvcPath->setMvcPathCorrector(
    new MVC_PathCorrector(
        $objConfig->getConfig('RULES_FOR_CORRECTION_REQUEST'),
        $objConfig->getConfig('DEFAULT_ROUTES'),
        $objConfig->getConfig('MVC_PARAM_FILTER')
    )
);

$arMvcWorkResult = array ();
foreach ($arMvcNodes as $strNodeName => $arMvcNode) {
    if ($arMvcNode[0] == MVC_Carcass::MAIN_REQUEST_ID) {
        $objMvcPath->setRequest($_SERVER['REQUEST_URI']);
    } else {
        $objMvcPath->setController($arMvcNode[0])
                   ->setMethod($arMvcNode[1])
                   ->setParam(@$arMvcNode[2]);
    }
    $objMvcBuilder->setMvcPath($objMvcPath);
    $arMvcWorkResult[$strNodeName] = $objMvcBuilder->build();
}

$page = $objMvcCarcass->parse($arMvcWorkResult);
print $objContainer->get('Hook_View')->parse($page);

// stop profiler
$xhprof_data = xhprof_disable();

# Сохраняем отчет и генерируем ссылку для его просмотра
include_once "xhprof_lib/utils/xhprof_lib.php";
include_once "xhprof_lib/utils/xhprof_runs.php";
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_test");
echo "Report: <a href=\"http://profiler/index.php?run=$run_id&source=xhprof_test\">profiler</a>";
echo "\n";